package tag

import (
	"BookStack/internal/domain/tag"
	"BookStack/pkg/utils"
)

// CreateTagRequest 创建标签请求
type CreateTagRequest struct {
	Name        string `json:"name" binding:"required,max=50"`
	Slug        string `json:"slug" binding:"required,max=100"`
	Type        int    `json:"type" binding:"required,oneof=1 2"` // 1=文章标签，2=书籍标签
	Description string `json:"description" binding:"max=200"`
}

// UpdateTagRequest 更新标签的DTO
type UpdateTagRequest struct {
	ID          uint   `json:"id" binding:"required" form:"id"`
	Name        string `json:"name" binding:"required" form:"name"`
	Slug        string `json:"slug" binding:"required,max=100" form:"slug"`
	Type        int    `json:"type" binding:"required,oneof=1 2" form:"type"`
	Description string `json:"description" form:"description"`
}

// TagResponse 标签响应
type TagResponse struct {
	ID          uint           `json:"id"`
	Name        string         `json:"name"`
	Slug        string         `json:"slug"`
	Type        int            `json:"type"`
	Description string         `json:"description"`
	UsageCount  int64          `json:"usage_count"`
	CreatedAt   utils.JSONTime `json:"created_at"`
	UpdatedAt   utils.JSONTime `json:"updated_at"`
}

// TagListQueryParams 标签列表查询参数
type TagListQueryParams struct {
	Page     int  `json:"page" form:"page"`
	PageSize int  `json:"page_size" form:"page_size" binding:"max=100"`
	Type     *int `json:"type" form:"type"` // 标签类型：1=文章标签，2=书籍标签
}

// TagListResponse 标签列表响应
type TagListResponse struct {
	Total int64          `json:"total"`
	List  []*TagResponse `json:"list"`
}

// TagStatResponse 标签使用统计响应
type TagStatResponse struct {
	ArticleCount int64 `json:"article_count"`
	BookCount    int64 `json:"book_count"`
	TotalCount   int64 `json:"total_count"`
}

// FromEntity 从实体转换为DTO
func FromEntity(entity *tag.Tag) *TagResponse {
	if entity == nil {
		return nil
	}
	return &TagResponse{
		ID:          entity.ID,
		Name:        entity.Name,
		Slug:        entity.Slug,
		Type:        entity.Type,
		Description: entity.Description,
		UsageCount:  entity.UsageCount,
		CreatedAt:   utils.JSONTime(entity.CreatedAt),
		UpdatedAt:   utils.JSONTime(entity.UpdatedAt),
	}
}

// ToEntity 从DTO转换为实体
func (req *CreateTagRequest) ToEntity() *tag.Tag {
	return &tag.Tag{
		Name:        req.Name,
		Slug:        req.Slug,
		Type:        req.Type,
		Description: req.Description,
	}
}
